<?php

date_default_timezone_set('Asia/Saigon');

class ManagerController extends Controller {

    public $layout = '//layouts/manager';

    public function actionIndex() {

        Yii::app()->clientScript->registerMetaTag('luật sư, luat su,luật doanh nghiệp, luatsukhongbiengioi, pháp luật, văn bản luật', 'keywords');
        Yii::app()->clientScript->registerMetaTag('luatsukhongbiengioi.org là website quy tụ tất cả luật sư tại việt nam, tra cứu thông tin pháp luật dễ dàng và nhanh chóng.', 'description');
        $this->pageTitle = 'Luật sư không biên giới, pháp luật';
        $this->render('index');
    }

    public function actionLichsugiaodich() {
        //$criteria = new CDbCriteria();

        $this->render('ca-nhan');
    }

    public function actionXemLichsugiaodichadmin() {
        $model = LogEdit::model()->findAll('flag<2');

        $this->render('ca-nhan', array(
            'model' => $model,));
    }

    public function actionAnloggiaodich($id) {
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_logedit($md->decrypt($id));
        $model->flag = 2;
        $model->save();
        $url = Yii::app()->createUrl('danh-sach-giao-dich');
        $this->redirect($url);

        $this->render('ca-nhan');
    }

    public function actionList_game() {
        $criteria = new CDbCriteria();
        $session = new CHttpSession;
        $session->open();

        //$userID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] < 99) {
            $this->redirect('http://luatsukhongbiengioi.org/');
        } else {
            $criteria->condition = ' flag <2';
        }
        $program = Program::model()->findAll($criteria);
        $this->render('ca-nhan', array(
            'program' => $program,));
    }

    public function actionList_slider() {
        $criteria = new CDbCriteria();
        $session = new CHttpSession;
        $session->open();


        $userID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] < 99) {
            $criteria->condition = ' flag<2 and UserID =' . $userID;
        } else {
            $criteria->condition = ' flag <3';
        }
        $slider = Slider::model()->findAll($criteria);
        $this->render('ca-nhan', array(
            'slider' => $slider,));
    }

    public function actionList_Panel() {
        $criteria = new CDbCriteria();
        $session = new CHttpSession;
        $session->open();


        $userID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] < 99) {
            $criteria->condition = ' flag<2 and uid =' . $userID;
        } else {
            $criteria->condition = ' flag <3';
        }
        $panel = Panel::model()->findAll($criteria);
        $this->render('ca-nhan', array(
            'panel' => $panel,));
    }

    public function actionList_Page() {
        $criteria = new CDbCriteria();
        $session = new CHttpSession;
        $session->open();


        $userID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] < 99) {
            $criteria->condition = ' flag<2 and uid =' . $userID;
        } else {
            $criteria->condition = ' flag <3';
        }
        $page = Page::model()->findAll($criteria);
        $this->render('ca-nhan', array(
            'page' => $page,));
    }

    public function actionList_People() {
        $results = '';
        if (isset($_POST['gid'])) {
            $id = $_POST['gid'];

            $sql = 'SELECT t1.id,t4.username,t1.position,t1.date_log,t2.title,t3.name ' .
                    ' FROM `program_detail` AS t1 ' .
                    ' INNER JOIN `program` AS t2 on t1.programid =t2.gid ' .
                    ' INNER JOIN `phanqua` AS t3 on t1.idqua =t3.id ' .
                    ' INNER JOIN  `user_tb` AS t4 ON t1.userid = t4.uid' .
                    ' where t1.programid=' . $id .
                    ' ORDER BY t1.id DESC';
            $dbCommand = Yii::app()->db->createCommand($sql);
            $results = $dbCommand->queryAll();
        } $this->render('ca-nhan', array(
            'results' => $results,));
    }

    public function actionMuaquangcao() {

        $this->pageTitle = 'Luật sư không biên giới';
        $model = new Apay(); //0974598708


        if (isset($_POST['Apay'])) {
            $model->attributes = $_POST['Apay'];
            $session = new CHttpSession;
            $session->open();
            if (!isset($session['thang'])) {
                $session['thang'] = 1;
            }
            $p = $session['thang'];
            $gia = $this->loadModel_config($model->OrderNo);
            if ((int) $p > 12) {
                $m = '2 năm';
            } elseif ((int) $p > 11) {
                $m = '1 năm';
            } else {
                $m = $p . ' tháng';
            }

            $value = $gia->value;

            $tota = $value * $p;
            $model->OrderCashAmount = $tota;
            $model->code = $model->OrderNo . date('Ymdhis');
            $arr = array($model->OrderNo . date('Ymdhis'), $tota, 'Thanh toán mua ' . $gia->name . ' online qua ví điện tử payoo tại chomoionline.com', $gia->name, number_format($value), $m, number_format($tota));
            $session['user_'] = $arr;
            $model->uid = $session['user_salesmart'][3];
            $model->daytime = new CDbExpression('NOW()');
            $model->state = 0;
            $model->thang = $p;
            if ($model->save()) {
                $session['chekout'] = $arr;
                $url = Yii::app()->createUrl('check-out');
                $this->redirect($url);
            }
        } else {
            $this->render('ca-nhan', array(
                'model' => $model,
            ));
        }
    }

    public function actionMuaquangcaoOk() {

        $this->pageTitle = 'Luật sư không biên giới';


        $model = 'Xin chúc mừng bạn đã mua quảng cáo thành công';
        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionMuaquangcaoFalse() {


        $this->pageTitle = 'Luật sư không biên giới';
        $model = 'Thanh toán thất bại. Vui lòng thử lại';
        $url = Yii::app()->createUrl('mua-quang-cao');
        $this->redirect($url);
    }

    public function loadModel_config($id) {
        $model = Config::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function actionTaikhoan() {
        //Home page
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'

        $this->pageTitle = 'Luật sư không biên giới';
        $canhan = 'true';
        $this->render('ca-nhan', array(
            'canhan' => $canhan,
        ));
    }

    public function actionDangky() {
      
         Yii::app()->clientScript->registerMetaTag('luật sư, luat su,Luật doanh nghiệp, luatsukhongbiengioi, pháp luật, văn bản luật, tư vấn pháp luật,tố tụng', 'keywords');
        Yii::app()->clientScript->registerMetaTag('đăng ký thành viên tại luatsukhongbiengioi.org để được hỗ trợ tốt nhất', 'description');
       
        $session = new CHttpSession;
        $session->open();

        if (!empty($session['user_salesmart'])) {
            $this->redirect('http://luatsukhongbiengioi.org/');
        }
        $model = new UserTb;

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        $ketqua = 0;
        $this->pageTitle = 'Đăng ký';
        if (isset($_POST['UserTb'])) {
            $model->attributes = $_POST['UserTb'];
            $model->flag = 1;
            $model->registerdate = new CDbExpression('NOW()');
            $model->pass = md5(md5($model->pass));
            $model->locaID = 1;
            if ($model->save()) {

                $this->redirect('thong-bao/1');
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionAddSlider() {

        $model = new Slider;
        $rnd = rand(0, 99999);
        $ketqua = 0;
        $this->pageTitle = 'Tạo slider';
        if (isset($_POST['Slider'])) {
            //$the_image = CUploadedFile::getInstance($model, 'photo');

            /*  if (is_object($the_image) && get_class($the_image) === 'CUploadedFile') {
              $model->photo = $the_image;
              } */
            $model->attributes = $_POST['Slider'];
            $uploadedFile = CUploadedFile::getInstance($model, 'photo');
            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
            if (!empty($uploadedFile)) {
                $model->photo = $fileName;
            }
            //$model->photo=CUploadedFile::getInstance($model,'photo');
            $model->flag = 0;
            $model->dateCreate = new CDbExpression('NOW()');
            $model->money = 0;
            $session = new CHttpSession;
            $session->open();

            $model->UserID = $session['user_salesmart'][3];
            if ($session['user_salesmart'][1] > 98) {
                $model->flag = 1;
            }

            /*
              foreach ($model as $item) {

              $item->UserTb->flag = $flag;
              } */
            if ($model->save()) {

                $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/product/slider/' . $model->photo);
                // $uploadFolder = Yii::getPathOfAlias('webroot') . DIRECTORY_SEPARATOR . 'upload' . DIRECTORY_SEPARATOR . substr(md5($the_image), 0, 1); //thư mục upload là thư mục nằm trong project
                //if (!is_dir($uploadFolder)) {
                // mkdir($uploadFolder, 0775, TRUE); //tạo thư mục nếu chưa tồn tại
                //}
                // $uploadFile = $uploadFolder . DIRECTORY_SEPARATOR . $the_image;
                //$model->photo->saveAs(Yii::getPathOfAlias('webroot') . '/upload/');
                //$this->redirect(array('view','id'=>$model->id));
                if ($session['user_salesmart'][1] < 99) {

                    $criteria1 = new CDbCriteria();
                    $criteria1->condition = ' UserID=' . $session['user_salesmart'][3];
                    $criteria1->limit = 1;
                    $criteria1->order = 'id DESC';

                    $list = Slider::model()->findAll($criteria1);

                    $log = new LogEdit();
                    foreach ($list as $item) {
                        $log->uid = $item->UserID;
                        $log->pid = $item->id;
                        $log->batdau = $item->dateCreate;
                        $log->kethuc = $item->dateExpend;
                        $log->batdaunew = $item->dateCreate;
                        $log->kethucnew = $item->dateExpend;
                        $log->title = $item->title;
                    }
                    $log->page = 1;
                    $log->day = new CDbExpression('NOW()');
                    $log->flag = 0;
                    $log->save();
                }
                $ketqua = 1;
                //$url = Yii::app()->createUrl('');
                //$this->redirect($url);
                $url = Yii::app()->createUrl('danh-sach-slider');
                $this->redirect($url);
                //$this->render('//manager/list-slider', array(
                // 'ketqua' => $ketqua,
                //));
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionList_Doitac() {

        $session = new CHttpSession;
        $session->open();


        $userID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model = Doitac::model()->findAll('flag<2');
            $this->render('ca-nhan', array(
                'model' => $model,));
        } else {
            $this->redirect('http://luatsukhongbiengioi.org/');
        }
    }

    public function actionAddDoitac() {

        $model = new Doitac();
        $rnd = rand(0, 99999);

        $this->pageTitle = 'Tạo Đối tác';
        if (isset($_POST['Doitac'])) {

            $model->attributes = $_POST['Doitac'];
            $uploadedFile = CUploadedFile::getInstance($model, 'photo');
            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
            if (!empty($uploadedFile)) {
                $model->photo = $fileName;
            }

            if ($model->save()) {
                $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/doitac/' . $model->photo);


                $url = Yii::app()->createUrl('danh-sach-doi-tac');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdateDoitac($id) {
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_doitac($md->decrypt($id));

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        $rnd = rand(0, 99999);
        $oldphoto = $model->photo;
        $up = 0;
        if (isset($_POST['Doitac'])) {
            $model->attributes = $_POST['Doitac'];
            $uploadedFile = CUploadedFile::getInstance($model, 'photo');
            $model->photo = $oldphoto;
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->photo = $fileName;
            }

            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/doitac/' . $model->photo);
                }
                $url = Yii::app()->createUrl('danh-sach-doi-tac');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteDoitac($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_doitac($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();

        $model->flag = 2;


        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-doi-tac');
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-doi-tac');
        $this->redirect($url);
        $this->render('ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionList_trungtam() {

        $session = new CHttpSession;
        $session->open();
        if ($session['user_salesmart'][1] > 98) {
            $model = Vanban::model()->findAll('flag<2');
            $this->render('ca-nhan', array(
                'model' => $model,));
        } else {
            $this->redirect('http://luatsukhongbiengioi.org/');
        }
    }

    public function actionAddtrungtam() {

        $model = new Vanban();


        $this->pageTitle = 'Tạo trung tâm - tổ chức';
        if (isset($_POST['Vanban'])) {

            $model->attributes = $_POST['Vanban'];
            $rnd = rand(0, 99999);
            $uploadedFile = CUploadedFile::getInstance($model, 'fileaddname');
            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
            if (!empty($uploadedFile)) {
                $model->fileaddname = $fileName;
            }

            $rnd1 = rand(0, 99999);
            $uploadedFile1 = CUploadedFile::getInstance($model, 'hinh');
            $fileName1 = "{$rnd1}-{$uploadedFile1}";  // random number + file name
            if (!empty($uploadedFile1)) {
                $model->hinh = $fileName1;
            }
            $model->datecreate = new CDbExpression('NOW()');
            $model->dateup = new CDbExpression('NOW()');

            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];
            if ($model->save()) {
                if (!empty($uploadedFile)) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/tailieu/' . $model->fileaddname);
                }
                if (!empty($uploadedFile1)) {
                    $uploadedFile1->saveAs(Yii::getPathOfAlias('webroot') . '/upload/image/' . $model->hinh);
                }

                $url = Yii::app()->createUrl('danh-sach-to-chuc');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdatetrungtam($id) {
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_trungtam($md->decrypt($id));

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        $oldphoto = $model->fileaddname;
        $oldphoto1 = $model->hinh;
        $up = 0;
        $up1 = 0;
        if (isset($_POST['Vanban'])) {
            $model->attributes = $_POST['Vanban'];
            $uploadedFile = CUploadedFile::getInstance($model, 'fileaddname');
            $model->fileaddname = $oldphoto;

            $rnd = rand(0, 99999);
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->fileaddname = $fileName;
            }

            $uploadedFile1 = CUploadedFile::getInstance($model, 'hinh');
            $model->hinh = $oldphoto1;
            $rnd1 = rand(0, 99999);
            if (!empty($uploadedFile1)) {
                $fileName1 = "{$rnd1}-{$uploadedFile1}";  // random number + file name
                $up1 = 1;
                $model->hinh = $fileName1;
            }


            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/tailieu/' . $model->fileaddname);
                }
                if ($up1 > 0) {
                    $uploadedFile1->saveAs(Yii::getPathOfAlias('webroot') . '/upload/image/' . $model->hinh);
                }
                $url = Yii::app()->createUrl('danh-sach-to-chuc');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeletetrungtam($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_trungtam($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();

        $model->flag = 2;


        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-to-chuc');
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-to-chuc');
        $this->redirect($url);
        $this->render('ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionDanhsachvanbanluat() {

        $session = new CHttpSession;
        $session->open();



        if ($session['user_salesmart'][1] > 98) {
            $model = Vanbanphapluat::model()->findAll('flag<2');
            $this->render('ca-nhan', array(
                'model' => $model,));
        } else {
            $this->redirect('http://luatsukhongbiengioi.org/');
        }
    }

    public function actionAddvanbanluat() {

        $model = new Vanbanphapluat();
        $rnd = rand(0, 99999);

        $this->pageTitle = 'Tạo văn bản pháp luật';
        if (isset($_POST['Vanbanphapluat'])) {

            $model->attributes = $_POST['Vanbanphapluat'];
            $uploadedFile = CUploadedFile::getInstance($model, 'file');
            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
            if (!empty($uploadedFile)) {
                $model->file = $fileName;
            }
            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];

            if ($model->save()) {
                 if (!empty($uploadedFile)) {
                $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/tailieu/' . $model->file);
                 }

                $url = Yii::app()->createUrl('danh-sach-van-ban-luat');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdatevanbanluat($id) {
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_vanbanluat($md->decrypt($id));

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        $rnd = rand(0, 99999);
        $oldphoto = $model->file;
        $up = 0;
        if (isset($_POST['Vanbanphapluat'])) {
            $model->attributes = $_POST['Vanbanphapluat'];
            $uploadedFile = CUploadedFile::getInstance($model, 'file');
            $model->file = $oldphoto;
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->file = $fileName;
            }

            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/tailieu/' . $model->file);
                }
                $url = Yii::app()->createUrl('danh-sach-van-ban-luat');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeletevanbanluat($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_vanbanluat($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();

        $model->flag = 2;


        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-van-ban-luat');
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-van-ban-luat');
        $this->redirect($url);
        $this->render('ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionDanhsachnews() {

        $session = new CHttpSession;
        $session->open();



        if ($session['user_salesmart'][1] > 98) {
            $model = News::model()->findAll('flag<2');
            $this->render('ca-nhan', array(
                'model' => $model,));
        } else {
            $this->redirect('http://luatsukhongbiengioi.org/');
        }
    }

    public function actionAddnews() {

        $model = new News();
        $rnd = rand(0, 99999);

        $this->pageTitle = 'Tạo tin tức sự kiện';
        if (isset($_POST['News'])) {

            $model->attributes = $_POST['News'];
            $uploadedFile = CUploadedFile::getInstance($model, 'photo');
            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
            if (!empty($uploadedFile)) {
                $model->photo = $fileName;
            }
            $session = new CHttpSession;
            $session->open();
            $model->ngay = new CDbExpression('NOW()');
            $model->uid = $session['user_salesmart'][3];

            if ($model->save()) {
                $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/news/' . $model->photo);


                $url = Yii::app()->createUrl('danh-sach-news');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdatenews($id) {
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_news($md->decrypt($id));

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        $rnd = rand(0, 99999);
        $oldphoto = $model->photo;
        $up = 0;
        if (isset($_POST['News'])) {
            $model->attributes = $_POST['News'];
            $uploadedFile = CUploadedFile::getInstance($model, 'photo');
            $model->photo = $oldphoto;
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->photo = $fileName;
            }

            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/news/' . $model->photo);
                }
                $url = Yii::app()->createUrl('danh-sach-news');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeletenews($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_news($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();

        $model->flag = 2;


        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-news');
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-news');
        $this->redirect($url);
        $this->render('ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionList_Giaitri() {

        $session = new CHttpSession;
        $session->open();


        $userID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model = Giaitri::model()->findAll('flag<2');
            $this->render('ca-nhan', array(
                'model' => $model,));
        } else {
            $this->redirect('http://luatsukhongbiengioi.org/');
        }
    }

    public function actionAddGiaitri() {

        $model = new Giaitri();
        $rnd = rand(0, 99999);

        $this->pageTitle = 'Tạo Giải Trí';
        if (isset($_POST['Giaitri'])) {

            $model->attributes = $_POST['Giaitri'];
            $uploadedFile = CUploadedFile::getInstance($model, 'logo');
            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
            if (!empty($uploadedFile)) {
                $model->logo = $fileName;
            }
            $session = new CHttpSession;
            $session->open();


            $model->uid = $session['user_salesmart'][3];

            if ($model->save()) {
                $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/giaitri/' . $model->logo);


                $url = Yii::app()->createUrl('danh-sach-giai-tri');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdateGiaitri($id) {
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_giaitri($md->decrypt($id));

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        $rnd = rand(0, 99999);
        $oldphoto = $model->logo;
        $up = 0;
        if (isset($_POST['Giaitri'])) {
            $model->attributes = $_POST['Giaitri'];
            $uploadedFile = CUploadedFile::getInstance($model, 'logo');
            $model->logo = $oldphoto;
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->logo = $fileName;
            }

            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/giaitri/' . $model->logo);
                }
                $url = Yii::app()->createUrl('danh-sach-giai-tri');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteGiaitri($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_giaitri($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();

        $model->flag = 2;


        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-giai-tri');
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-giai-tri');
        $this->redirect($url);
        $this->render('ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionAddPage() {

        $model = new Page();

        $this->pageTitle = 'Tạo Page';
        if (isset($_POST['Page'])) {

            $model->attributes = $_POST['Page'];

            $model->datecreate = new CDbExpression('NOW()');
            $model->dateup = new CDbExpression('NOW()');

            //$model->dateCreate = new CDbExpression('NOW()');
            //$model->money = 0;
            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];
            if ($session['user_salesmart'][1] < 99) {
                $model->flag = 0;
            }

            if ($model->save()) {

                if ($session['user_salesmart'][1] < 99) {

                    $criteria1 = new CDbCriteria();
                    $criteria1->condition = ' uid=' . $session['user_salesmart'][3];
                    $criteria1->limit = 1;
                    $criteria1->order = 'id DESC';

                    $list = Page::model()->findAll($criteria1);

                    $log = new LogEdit();
                    foreach ($list as $item) {
                        $log->uid = $item->uid;
                        $log->pid = $item->id;
                        $log->batdau = $item->datecreate;
                        $log->kethuc = $item->dateup;
                        $log->batdaunew = $item->datecreate;
                        $log->kethucnew = $item->dateup;
                        $log->title = $item->title;
                    }
                    $log->page = 2;
                    $log->day = new CDbExpression('NOW()');
                    $log->flag = 0;
                    $log->save();
                }

                $url = Yii::app()->createUrl('danh-sach-page');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdatePage($id) {
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_page($md->decrypt($id));

        if (isset($_POST['Page'])) {
            $model->attributes = $_POST['Page'];
            $session = new CHttpSession;
            $session->open();

            if ($session['user_salesmart'][1] < 99) {
                $model->flag = 0;
            }

            if ($model->save()) {

                if ($session['user_salesmart'][1] < 99) {

                    $criteria1 = new CDbCriteria();
                    $criteria1->condition = 'id=' . $md->decrypt($id);
                    //$criteria1->limit = 1;
                    //$criteria1->order = 'id DESC';

                    $list = Page::model()->findAll($criteria1);

                    $log = new LogEdit();
                    foreach ($list as $item) {
                        $log->uid = $item->uid;
                        $log->pid = $item->id;
                        $log->batdau = $item->datecreate;
                        $log->kethuc = $item->dateup;
                        $log->batdaunew = $item->datecreate;
                        $log->kethucnew = $item->dateup;
                        $log->title = $item->title;
                    }
                    $log->page = 2;
                    $log->day = new CDbExpression('NOW()');
                    $log->flag = 0;
                    $log->save();
                }

                $url = Yii::app()->createUrl('danh-sach-page');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeletePage($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_page($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        $model->uid = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-page');
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-page');
        $this->redirect($url);
        $this->render('ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionAddPanel() {

        $model = new Panel();
        $rnd = rand(0, 99999);
        $ketqua = 0;
        $this->pageTitle = 'Tạo panel';
        if (isset($_POST['Panel'])) {

            $model->attributes = $_POST['Panel'];
            $uploaded = CUploadedFile::getInstance($model, 'photo');
            $fileName = "{$rnd}-{$uploaded}";  // random number + file name
            if (!empty($uploaded)) {
                $model->photo = $fileName;
            }

            $model->flag = 0;
            //$model->dateCreate = new CDbExpression('NOW()');
            //$model->money = 0;
            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];
            if ($session['user_salesmart'][1] > 98) {
                $model->flag = 1;
            }
            if ($session['user_salesmart'][1] < 99) {
                $kiemtra = Apay::model()->findAll(' `uid`=' . $session['user_salesmart'][3] . ' and  `OrderNo`=' . $model->type . ' and `dayend` >= "' . date('Y-m-d') . '" order by id DESC');
                if (empty($kiemtra)) {
                    $ketqua = 1;
                }
            }
            if ($ketqua == 0) {
                if ($model->save()) {
                    $uploaded->saveAs(Yii::getPathOfAlias('webroot') . '/upload/quangcao/' . $model->photo);

                    if ($session['user_salesmart'][1] < 99) {

                        $criteria1 = new CDbCriteria();
                        $criteria1->condition = ' uid=' . $session['user_salesmart'][3];
                        $criteria1->limit = 1;
                        $criteria1->order = 'id DESC';

                        $list = Panel::model()->findAll($criteria1);

                        $log = new LogEdit();
                        foreach ($list as $item) {
                            $log->uid = $item->uid;
                            $log->pid = $item->id;
                            $log->batdau = $item->dateCreate;
                            $log->kethuc = $item->dateEpend;
                            $log->batdaunew = $item->dateCreate;
                            $log->kethucnew = $item->dateEpend;
                            $log->title = $item->name;
                        }
                        $log->page = 3;
                        $log->day = new CDbExpression('NOW()');
                        $log->flag = 0;
                        $log->save();
                    }

                    $url = Yii::app()->createUrl('danh-sach-panel');
                    $this->redirect($url);
                }
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
            'ketqua' => $ketqua,
        ));
    }

    public function actionUpdatePanel($id) {
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_panel($md->decrypt($id));

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        $rnd = rand(0, 99999);
        $oldphoto = $model->photo;
        $batdau = $model->dateCreate;
        $kethuc = $model->dateEpend;
        $up = 0;
        if (isset($_POST['Panel'])) {
            $model->attributes = $_POST['Panel'];
            $uploadedFile = CUploadedFile::getInstance($model, 'photo');
            $model->photo = $oldphoto;
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->photo = $fileName;
            }
            $session = new CHttpSession;
            $session->open();


            if ($session['user_salesmart'][1] < 99) {
                $model->flag = 0;
            }
            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/quangcao/' . $model->photo);
                }

                if ($session['user_salesmart'][1] < 99) {

                    $criteria1 = new CDbCriteria();
                    $criteria1->condition = ' id=' . $md->decrypt($id);
                    // $criteria1->limit = 1;
                    //$criteria1->order = 'id DESC';

                    $list = Panel::model()->findAll($criteria1);

                    $log = new LogEdit();
                    foreach ($list as $item) {
                        $log->uid = $item->uid;
                        $log->pid = $item->id;
                        $log->batdau = $item->dateCreate;
                        $log->kethuc = $item->dateEpend;
                        $log->batdaunew = $item->dateCreate;
                        $log->kethucnew = $item->dateEpend;
                        $log->title = $item->name;
                    }
                    $log->page = 3;
                    $log->day = new CDbExpression('NOW()');
                    $log->flag = 0;
                    $log->save();
                }


                $url = Yii::app()->createUrl('danh-sach-panel');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeletePanel($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_panel($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        $model->uid = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-panel');
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-panel');
        $this->redirect($url);
        $this->render('ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionList_Product() {
        $criteria = new CDbCriteria();
        $session = new CHttpSession;
        $session->open();

        $userID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] < 99) {
            $criteria->condition = ' flag<2 and uid =' . $userID;
        } else {
            $criteria->condition = ' flag <3';
        }
        $product = Product::model()->findAll($criteria);
        $this->render('ca-nhan', array(
            'product' => $product,));
    }

    public function actionAddProduct() {

        $model = new Product();
        $rnd = rand(0, 99999);
        $ketqua = 0;
        $this->pageTitle = 'Tạo panel';
        if (isset($_POST['Product'])) {

            $model->attributes = $_POST['Product'];
            $uploaded = CUploadedFile::getInstance($model, 'avatar');
            $fileName = "{$rnd}-{$uploaded}";  // random number + file name
            if (!empty($uploaded)) {
                $model->avatar = $fileName;
            }

            $model->pr_datecreate = new CDbExpression('NOW()');
            $model->pr_dateup = new CDbExpression('NOW()');

            //$model->dateCreate = new CDbExpression('NOW()');
            //$model->money = 0;
            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];
            if ($session['user_salesmart'][1] < 98) {
                $model->flag = 0;
            }
            $model->position = 1;
            if ($model->save()) {
                $uploaded->saveAs(Yii::getPathOfAlias('webroot') . '/upload/product/avata/' . $model->avatar);

                if ($session['user_salesmart'][1] < 99) {

                    $criteria1 = new CDbCriteria();
                    $criteria1->condition = ' uid=' . $session['user_salesmart'][3];
                    $criteria1->limit = 1;
                    $criteria1->order = 'prid DESC';

                    $list = Product::model()->findAll($criteria1);

                    $log = new LogEdit();
                    foreach ($list as $item) {
                        $log->uid = $item->uid;
                        $log->pid = $item->prid;
                        $log->batdau = $item->pr_stardate;
                        $log->kethuc = $item->pr_enddate;
                        $log->batdaunew = $item->pr_stardate;
                        $log->kethucnew = $item->pr_enddate;
                        $log->title = $item->pr_name;
                    }
                    $log->page = 4;
                    $log->day = new CDbExpression('NOW()');
                    $log->flag = 0;
                    $log->save();
                }

                $ketqua = 1;
                $url = Yii::app()->createUrl('danh-sach-san-pham');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdateProduct($id) {
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_product($md->decrypt($id));

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        $rnd = rand(0, 99999);
        $oldphoto = $model->avatar;
        $batdau = $model->pr_stardate;
        $kethuc = $model->pr_stardate;
        $up = 0;
        if (isset($_POST['Product'])) {
            $model->attributes = $_POST['Product'];
            $uploadedFile = CUploadedFile::getInstance($model, 'avatar');
            $model->avatar = $oldphoto;
            $session = new CHttpSession;
            $session->open();

            // $model->uid = $session['user_salesmart'][3];
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->avatar = $fileName;
            }
            if ($session['user_salesmart'][1] < 99) {
                $model->flag = 0;
            }
            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/product/avata/' . $model->avatar);
                }
                if ($session['user_salesmart'][1] < 99) {

                    $criteria1 = new CDbCriteria();
                    $criteria1->condition = ' prid=' . $md->decrypt($id);
                    //$criteria1->limit = 1;
                    // $criteria1->order = 'id DESC';

                    $list = Product::model()->findAll($criteria1);

                    $log = new LogEdit();
                    foreach ($list as $item) {
                        $log->uid = $item->uid;
                        $log->pid = $item->prid;
                        $log->batdau = $item->pr_stardate;
                        $log->kethuc = $item->pr_enddate;
                        $log->batdaunew = $item->pr_stardate;
                        $log->kethucnew = $item->pr_enddate;
                        $log->title = $item->pr_name;
                    }
                    $log->page = 4;
                    $log->day = new CDbExpression('NOW()');
                    $log->flag = 0;
                    $log->save();
                }

                $url = Yii::app()->createUrl('danh-sach-san-pham');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteProduct($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_product($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        $model->uid = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-san-pham');
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-san-pham');
        $this->redirect($url);
        $this->render('ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionAddGame() {

        $model = new Program;

        $ketqua = 0;
        $this->pageTitle = 'Tạo chương trình';
        if (isset($_POST['Program'])) {

            $model->attributes = $_POST['Program'];
            if ($model->save()) {
                $ketqua = 1;
                $url = Yii::app()->createUrl('Chuong-trinh-game-show');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionAddPhanqua() {

        $model = new Phanqua();

        $ketqua = 0;
        $this->pageTitle = 'Tạo phần quà';
        if (isset($_POST['Phanqua'])) {

            $model->attributes = $_POST['Phanqua'];
            if ($model->save()) {
                $ketqua = 1;
                $url = Yii::app()->createUrl('cai-dat-qua');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdatePhanqua($id) {

        $this->pageTitle = 'Quản lý phần quà';
        $thongbao = '';
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_phanqua($md->decrypt($id));

        if (isset($_POST['Phanqua'])) {
            $model->attributes = $_POST['Phanqua'];
            if ($model->save()) {
                $thongbao = 'Thông tin của bạn đã được cập nhật';
            }
        }
        $this->render('ca-nhan', array(
            'model' => $model,
            'thongbao' => $thongbao,
        ));
    }

    public function actionUpdateProgram($id) {

        $this->pageTitle = 'Quản lý chương trình';
        $thongbao = '';
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_program($md->decrypt($id));

        if (isset($_POST['Program'])) {
            $model->attributes = $_POST['Program'];
            if ($model->save()) {
                $thongbao = 'Thông tin của bạn đã được cập nhật';
            }
        }
        $this->render('ca-nhan', array(
            'model' => $model,
            'thongbao' => $thongbao,
        ));
    }

    public function actionUpdateUser() {

        $this->pageTitle = 'Thông tin cá nhân';
        $session = new CHttpSession;
        $session->open();
        $id = $session['user_salesmart'][3];
        $thongbao = '';
        $model = $this->loadModel_user($id);
        $pass = $model->pass;
        $registerdate = $model->registerdate;
        $email = $model->email;
        if (isset($_POST['UserTb'])) {
            $model->attributes = $_POST['UserTb'];

            $model->pass = $pass;
            $model->registerdate = $registerdate;
            $model->email = $email;
            if ($model->save()) {
                $thongbao = 'Thông tin của bạn đã được cập nhật';
            }
        }
        $this->render('ca-nhan', array(
            'model' => $model,
            'thongbao' => $thongbao,
        ));
    }

    public function actionChangepass() {

        $this->pageTitle = 'Luật sư không biên giới';
        $session = new CHttpSession;
        $session->open();
        $id = $session['user_salesmart'][3];
        $thongbao = '';



        if (isset($_POST['pass']) && isset($_POST['rpwd']) && isset($_POST['olpass'])) {

            if ($_POST['pass'] === $_POST['rpwd']) {
                $p = $_POST['pass'];
                $rp = $_POST['olpass'];
                $model = $this->loadModel_user($id);
                if ($model->pass === md5(md5($rp))) {
                    $model->pass = md5(md5($p));
                    if ($model->save()) {
                        $thongbao = 'Thông tin của bạn đã được cập nhật';
                        $this->redirect('ca-nhan', array(
                            'thongbao' => $thongbao,));
                    }
                } else {
                    $thongbao = 'Mật khẩu cũ không đúng';
                }
            } else {
                $thongbao = 'Mật khẩu nhập lại không đúng';
            }
        }
        $this->render('ca-nhan', array(
            'thongbao' => $thongbao,
        ));
    }

    public function actionUpdateSlider($id) {
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_slider($md->decrypt($id));

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        $rnd = rand(0, 99999);
        $oldphoto = $model->photo;
        $batdau1 = $model->dateCreate;
        $kethuc1 = $model->dateExpend;
        $up = 0;
        if (isset($_POST['Slider'])) {
            $model->attributes = $_POST['Slider'];
            $uploadedFile = CUploadedFile::getInstance($model, 'photo');
            $model->photo = $oldphoto;
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->photo = $fileName;
            }
            $session = new CHttpSession;
            $session->open();
            if ($session['user_salesmart'][1] < 99) {
                $model->flag = 0;
            }
            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/product/slider/' . $model->photo);
                }
                if ($session['user_salesmart'][1] < 99) {

                    $criteria1 = new CDbCriteria();
                    $criteria1->condition = ' id=' . $md->decrypt($id);
                    //$criteria1->limit = 1;
                    //$criteria1->order = 'id DESC';

                    $list = Slider::model()->findAll($criteria1);

                    $log = new LogEdit();
                    foreach ($list as $item) {
                        $log->uid = $item->UserID;
                        $log->pid = $item->id;
                        $log->batdau = $item->dateCreate;
                        $log->kethuc = $item->dateExpend;
                        $log->batdaunew = $item->dateCreate;
                        $log->kethucnew = $item->dateExpend;
                        $log->title = $item->title;
                    }
                    $log->page = 1;
                    $log->day = new CDbExpression('NOW()');
                    $log->flag = 0;
                    $log->save();
                }
                $url = Yii::app()->createUrl('danh-sach-slider');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteSlider($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_slider($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        $model->UserID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-slider');
            $this->redirect($url);
        }
        $url = Yii::app()->createUrl('danh-sach-slider');
        $this->redirect($url);
        $this->render('ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionDeleteProgam($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_program($md->decrypt($id));
        $model->flag = 3;

        if ($model->save()) {
            $url = Yii::app()->createUrl('Chuong-trinh-game-show');
            $this->redirect($url);
        }
        $url = Yii::app()->createUrl('Chuong-trinh-game-show');
        $this->redirect($url);
        $this->render('ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionXoaPhanqua($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_phanqua($md->decrypt($id));
        $model->flag = 3;

        if ($model->save()) {
            $url = Yii::app()->createUrl('cai-dat-qua');
            $this->redirect($url);
        }
        $url = Yii::app()->createUrl('cai-dat-qua');
        $this->redirect($url);
        $this->render('ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    // Uncomment the following methods and override them if needed
    /*
      public function filters()
      {
      // return the filter configuration for this controller, e.g.:
      return array(
      'inlineFilterName',
      array(
      'class'=>'path.to.FilterClass',
      'propertyName'=>'propertyValue',
      ),
      );
      }

      public function actions()
      {
      // return external action classes, e.g.:
      return array(
      'action1'=>'path.to.ActionClass',
      'action2'=>array(
      'class'=>'path.to.AnotherActionClass',
      'propertyName'=>'propertyValue',
      ),
      );
      }
     */

    public function loadModel_phanqua($id) {
        $model = Phanqua::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_program($id) {
        $model = Program::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_panel($id) {
        $model = Panel::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_logedit($id) {
        $model = LogEdit::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_doitac($id) {
        $model = Doitac::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_trungtam($id) {
        $model = Vanban::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_vanbanluat($id) {
        $model = Vanbanphapluat::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_news($id) {
        $model = News::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_giaitri($id) {
        $model = Giaitri::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_page($id) {
        $model = Page::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_slider($id) {
        $model = Slider::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_product($id) {
        $model = Product::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_user($id) {
        $model = UserTb::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    //shop shop 
    public function actionDynamicvalue() {
        $session = new CHttpSession;
        $session->open();
        $session['trapos'] = '1';
        $data = Config::model()->findAll('flag=1 and id=:id', array(':id' => (int) $_POST['id']));
        $data1 = CHtml::listData($data, 'id', 'value');

        foreach ($data1 as $value => $name) {
            //echo CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
            $session['loaiqc'] = $name;

            $tien = $name;
            if (isset($session['thang'])) {
                $thang = $session['thang'];
                $tien = $thang * $tien;
            }
            echo CHtml::textField('Text', number_format($tien), array('id' => 'qcso', 'width' => 100, 'maxlength' => 100, 'readOnly' => true));
        }
    }

    public function actionDynamicvalue1() {
        $session = new CHttpSession;
        $session->open();
        if (isset($session['loaiqc'])) {
            if (!empty($session['loaiqc'])) {
                $vlu = $session['loaiqc'];
                $session['thang'] = (int) $_POST['id'];
                $k = (int) $_POST['id'] * (int) $vlu;
                echo CHtml::textField('Text', number_format(CHtml::encode($k)), array('id' => 'qcso', 'width' => 100, 'maxlength' => 100, 'readOnly' => true));
            }
        }
    }

    public function actionDynamicvalueloai() {
        //$session = new CHttpSession;
        // $session->open();
        // $session['hinhthuc'] = (int) $_POST['id'];

        $k = (int) $_POST['id'];
        if ($k == 999) {

            echo '<input value="0" style="width:80px;" name="Product[discount]" id="Product_discount" type="text">';
        } else {
            echo '<input type="hidden" value="' . $k . '" style="width:80px;" name="Product[discount]" id="Product_discount" type="text">';
        }
    }

    public function actionDynamicvalueloaipram() {
        $session = new CHttpSession;
        $session->open();
        // $session['hinhthuc'] = (int) $_POST['id'];

        $k = (int) $_POST['key'];
        //$session['valueh']=$k;
        if ($k == 999) {
            echo '<input style="width:80px;" name="Parner[discount]" id="Parner_discount" type="text">';
        } else {
            echo '<input type="hidden" value="' . $k . '" style="width:80px;" name="Parner[discount]" id="Parner_discount" type="text">';
        }
    }

    public function actionDynamicMenushop() {
        $data = ShopMenu::model()->findAll('parentID=0 and shopid=:shopid', array(':shopid' => (int) $_POST['id']));
        $value0 = 0;
        $data = CHtml::listData($data, 'id', 'title');
        echo CHtml::tag('option', array('value' => $value0), '----- Chọn menu cha -----', true);
        foreach ($data as $value => $name) {
            echo CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
        }
    }

    public function actionDynamicCategory() {
        $data = ShopNavbar::model()->findAll('parent=0 and shopid=:shopid', array(':shopid' => (int) $_POST['id']));
        $value0 = 0;
        $data = CHtml::listData($data, 'id', 'title');
        echo CHtml::tag('option', array('value' => $value0), '----- Chọn danh mục cha -----', true);
        foreach ($data as $value => $name) {
            echo CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
        }
        /*
          if (!empty($data)) {
          foreach ($data as $item) {
          echo CHtml::tag('option', array('value' => $item->disID), $item->disName, true);
          }
         */
    }

    public function actionDynamicProduct() {
        $data = ShopNavbar::model()->findAll('shopid=:shopid', array(':shopid' => (int) $_POST['id']));
        foreach ($data as $item) {
            if ($item->parent > 0) {
                $item->title = '   + ' . $item->title;
            }
        }
        $value0 = 0;
        $data = CHtml::listData($data, 'id', 'title');
        //echo CHtml::tag('option', array('value' => $value0), '----- Chọn danh mục cha -----', true);
        foreach ($data as $value => $name) {
            echo CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
        }
    }

    public function actionDynamicCity() {
        $data = ShopCity::model()->findAll('flag=1 and shopid=:shopid', array(':shopid' => (int) $_POST['id']));
        // $value0 = 0;
        $data = CHtml::listData($data, 'id', 'cityName');
        //echo CHtml::tag('option', array('value' => $value0), '----- Chọn danh mục cha -----', true);
        foreach ($data as $value => $name) {
            echo CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
        }
    }

    public function actionList_Parner() {
        $criteria = new CDbCriteria();
        $session = new CHttpSession;
        $session->open();
        if (!isset($session['user_salesmart'])) {
            $this->redirect('http://luatsukhongbiengioi.org/');
        }
        $userID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] < 99) {
            $criteria->condition = ' flag in(0,1,4) and uid =' . $userID;
        } else {
            $criteria->condition = ' flag in(0,1,2,4)';
        }

        $model = Parner::model()->findAll($criteria);
        $this->render('///manager/ca-nhan', array(
            'model' => $model,));
    }

    public function actionAddParner() {

        $model = new Parner();
        $rnd = rand(0, 99999);
        $this->pageTitle = 'Thông tin luật sư';
        if (isset($_POST['Parner'])) {

            $model->attributes = $_POST['Parner'];
            $uploadedFile = CUploadedFile::getInstance($model, 'logo');
            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
            if (!empty($uploadedFile)) {
                $model->logo = $fileName;
                 $model->photo = $fileName;
            }
          
           
            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];
            if ($session['user_salesmart'][1] < 99) {
                $model->flag = 0;
            }
            $code = date('Ymdhis');

            $model->code = $code;


            if ($model->save()) {

                if (!empty($uploadedFile)) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/avata/' . $model->logo);
                }

               



                $url = 'danh-sach-luat-su';
                die('<script type="text/javascript">window.location.href="' . $url . '";</script>');
                //header('Location: http://chomoionline.com/danh-sach-shop');
                // $url = Yii::app()->createUrl('http://chomoionline.com/danh-sach-shop');
                //$this->redirect('http://chomoionline.com/danh-sach-shop');
            }
        }

        $this->render('///manager/ca-nhan', array(
            'model' => $model,
        ));
    }

     public function actionUpdateParner() {
        Yii::import('application.controllers.encode_decode');
        
        $session = new CHttpSession;
        $session->open();
        $uid = $session['user_salesmart'][3];
        
            $model = Parner::model()->findAll('uid='.$uid);
        
        
  
        $id=0;
        foreach ($model as $item){
          
            $id=$item->id;
        }
        
        if ($id<1) {
             $this->redirect('http://luatsukhongbiengioi.org/thong-tin-luat-su');
         
        } else {
            $model = $this->loadModel_parne($id);
            $oldlogo ='';
            $rnd = rand(0, 99999);
            if(!empty($model->logo)){
            $oldlogo = $model->logo;
            }
            if (isset($_POST['Parner'])) {
                $model->attributes = $_POST['Parner'];
                $uploadedFile = CUploadedFile::getInstance($model, 'logo');
                
                $model->logo = $oldlogo;
                $up = 0;
                if (!empty($uploadedFile)) {
                    $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                    $up = 1;
                    $model->logo = $fileName;
                }



                $session = new CHttpSession;
                $session->open();


                if ($session['user_salesmart'][1] < 99) {
                    $model->flag = 0;
                }

                if ($model->save()) {
                    if ($up > 0) {
                        $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/avata/' . $model->logo);
                    }



                    header('Location: http://luatsukhongbiengioi.org/edit-thong-tin-luat-su');
                }
            }

            
        }
        
        $this->render('///manager/ca-nhan', array(
                'model' => $model,
            ));
    }
    
    public function actionUpdateParnerad($id) {
         Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
  
        $session = new CHttpSession;
        $session->open();

            $model = $this->loadModel_parne($md->decrypt($id));
            $oldlogo ='';
            $rnd = rand(0, 99999);
            if(!empty($model->logo)){
            $oldlogo = $model->logo;
            }
            if (isset($_POST['Parner'])) {
                $model->attributes = $_POST['Parner'];
                $uploadedFile = CUploadedFile::getInstance($model, 'logo');
                
                $model->logo = $oldlogo;
                $up = 0;
                if (!empty($uploadedFile)) {
                    $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                    $up = 1;
                    $model->logo = $fileName;
                }

                if ($session['user_salesmart'][1] < 99) {
                    $model->flag = 0;
                }

                if ($model->save()) {
                    if ($up > 0) {
                        $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/avata/' . $model->logo);
                    }



                    header('Location: http://luatsukhongbiengioi.org/edit-thong-tin-luat-su');
                }
            

            
        }
        
        $this->render('///manager/ca-nhan', array(
                'model' => $model,
            ));
    }

    public function actionDeleteParner($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_parne($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();

        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }



        if ($model->save()) {

            header('Location: http://luatsukhongbiengioi.org/danh-sach-luat-su');
        }

        //$url = Yii::app()->createUrl('http://chomoionline.com/danh-sach-shop');
        header('Location: http://luatsukhongbiengioi.org/danh-sach-luat-su');
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionList_Shop() {
        $criteria = new CDbCriteria();
        $session = new CHttpSession;
        $session->open();
        if (!isset($session['user_salesmart'])) {
            $this->redirect('http://luatsukhongbiengioi.org/');
        }
        $userID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] < 99) {
            $criteria->condition = ' flag in(1,4) and uid =' . $userID;
        } else {
            $criteria->condition = ' flag in(1,2,4)';
        }
        $shop = Shop::model()->findAll($criteria);
        $this->render('///manager/ca-nhan', array(
            'shop' => $shop,));
    }

    public function actionAddShop() {

        $model = new Shop();
        $rnd = rand(0, 99999);
        $this->pageTitle = 'Tạo Gian Hàng';
        if (isset($_POST['Shop'])) {

            $model->attributes = $_POST['Shop'];
            $uploadedFile = CUploadedFile::getInstance($model, 'logo');
            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
            if (!empty($uploadedFile)) {
                $model->logo = $fileName;
            }
            $rnd = rand(0, 99999);
            $uploadedFile1 = CUploadedFile::getInstance($model, 'avata');
            $fileName1 = "{$rnd}-{$uploadedFile1}";  // random number + file name
            if (!empty($uploadedFile1)) {
                $model->avata = $fileName1;
            }

            $model->datecreate = new CDbExpression('NOW()');

            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];
            //if ($session['user_salesmart'][1] < 99) {
            // $model->flag = 0;
            // }

            if ($model->save()) {
                $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/icon/' . $model->logo);
                $uploadedFile1->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/icon/' . $model->avata);
                $url = Yii::app()->createUrl('danh-sach-shop');
                $this->redirect($url);
            }
        }

        $this->render('///manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdateShop($id) {
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_shop($md->decrypt($id));
        $rnd = rand(0, 99999);
        $oldphoto = $model->logo;
        $oldphoto1 = $model->avata;
        if (isset($_POST['Shop'])) {
            $model->attributes = $_POST['Shop'];
            $uploadedFile = CUploadedFile::getInstance($model, 'logo');
            $model->logo = $oldphoto;
            $up = 0;
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->logo = $fileName;
            }
            $rnd = rand(0, 99999);
            $uploadedFile1 = CUploadedFile::getInstance($model, 'avata');
            $model->avata = $oldphoto1;
            $up1 = 0;
            if (!empty($uploadedFile1)) {
                $fileName1 = "{$rnd}-{$uploadedFile1}";  // random number + file name
                $up1 = 1;
                $model->avata = $fileName1;
            }

            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/icon/' . $model->logo);
                }
                if ($up1 > 0) {
                    $uploadedFile1->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/icon/' . $model->avata);
                }
                $url = Yii::app()->createUrl('danh-sach-shop');
                $this->redirect($url);
            }
        }

        $this->render('///manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteShop($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_shop($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        $model->uid = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-shop');
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-shop');
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionListPageshop() {
        $this->pageTitle = 'Quản lý bài viết';
        $this->render('//manager/ca-nhan', array(
        ));
    }

    public function actionDsPageshop($id) {

        $session = new CHttpSession;
        $session->open();

        $model = ShopPage::model()->findAll('flag in(0,1) and shopid=' . $id . ' and uid=' . $session['user_salesmart'][3]);

        $this->pageTitle = 'Danh sách bài viét';
        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

     public function actionList_lienhels() {
        $criteria = new CDbCriteria();
        $session = new CHttpSession;
        $session->open();
        if (!isset($session['user_salesmart'])) {
            $this->redirect('http://luatsukhongbiengioi.org/');
        }
        
        if ($session['user_salesmart'][1] < 99) {
            $this->redirect('http://luatsukhongbiengioi.org/');
        } else {
            $criteria->condition = ' flag in(0,1,2)';
        }
        $model = Lienhe::model()->findAll($criteria);
        $this->render('///manager/ca-nhan', array(
            'model' => $model,));
    }
    
    public function actionAddlienhels() {

        $model = new Lienhe();

        $this->pageTitle = 'Liên hệ luật sư';
        if (isset($_POST['Lienhe'])) {

            $model->attributes = $_POST['Lienhe'];


            //$model->dateCreate = new CDbExpression('NOW()');
            $model->flag = 0;
         
            if ($model->save()) {
                $this->redirect('thong-bao/7');
          
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdatelienhels($id) {
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_lienheluatsu($md->decrypt($id));
        if (isset($_POST['Lienhe'])) {
            $model->attributes = $_POST['Lienhe'];
            if ($model->save()) {

                $url = Yii::app()->createUrl('danh-sach-lien-he-luat-su/' . $model->id);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeletelienhels($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_lienheluatsu($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        //$model->uid = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-lien-he-luat-su/' . $model->id);
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-lien-he-luat-su/' . $model->id);
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

     public function actionList_lienheqc() {
        $criteria = new CDbCriteria();
        $session = new CHttpSession;
        $session->open();
        if (!isset($session['user_salesmart'])) {
            $this->redirect('http://luatsukhongbiengioi.org/');
        }
        
        if ($session['user_salesmart'][1] < 99) {
            $this->redirect('http://luatsukhongbiengioi.org/');
        } else {
            $criteria->condition = ' flag in(0,1,2)';
        }
        $model = Lienhequangcao::model()->findAll($criteria);
        $this->render('///manager/ca-nhan', array(
            'model' => $model,));
    }
     public function actionAddlienheqc() {

        $model = new Lienhequangcao();

        $this->pageTitle = 'Liên hệ luật sư';
        if (isset($_POST['Lienhequangcao'])) {

            $model->attributes = $_POST['Lienhequangcao'];


            //$model->dateCreate = new CDbExpression('NOW()');
            $model->flag = 0;
         
            if ($model->save()) {
                $this->redirect('thong-bao/8');
          
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdatelienheqc($id) {
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_lienhequangcao($md->decrypt($id));
        if (isset($_POST['Lienhequangcao'])) {
            $model->attributes = $_POST['Lienhequangcao'];
            if ($model->save()) {

                $url = Yii::app()->createUrl('danh-sach-lien-he-quang-cao/' . $model->id);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeletelienheqc($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_lienhequangcao($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        //$model->uid = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-lien-he-quang-cao/' . $model->id);
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-lien-he-quang-cao/' . $model->id);
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }
    
    
    
    
    
    
    
    
    
    public function actionAddCity() {

        $model = new ShopCity();

        $this->pageTitle = 'Thêm tỉnh/thành';
        if (isset($_POST['ShopCity'])) {

            $model->attributes = $_POST['ShopCity'];
            $session = new CHttpSession;
            $session->open();
            $model->shopid = $session['user_salesmart'][3];

            if ($model->save()) {

                $url = Yii::app()->createUrl('danh-sach-city');
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdateCity($id) {
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_Cityshop($md->decrypt($id));
        if (isset($_POST['ShopCity'])) {
            $model->attributes = $_POST['ShopCity'];
            if ($model->save()) {

                $url = Yii::app()->createUrl('danh-sach-city');
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteCity($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_Cityshop($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();

        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-city');
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-city');
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionListMenu() {
        $this->pageTitle = 'Quản lý menu';
        $this->render('//manager/ca-nhan', array(
        ));
    }

    public function actionDsMenu($id) {

        $session = new CHttpSession;
        $session->open();

        $model = ShopMenu::model()->findAll('flag in(0,1) and shopid=' . $id . ' and uid=' . $session['user_salesmart'][3]);

        $this->pageTitle = 'Thêm menu';
        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionAddMenu() {

        $model = new ShopMenu();

        $this->pageTitle = 'Thêm menu';
        if (isset($_POST['ShopMenu'])) {

            $model->attributes = $_POST['ShopMenu'];
            $session = new CHttpSession;
            $session->open();
            $model->uid = $session['user_salesmart'][3];
            if ($model->parentID == NULL) {
                $model->parentID = 0;
            }

            if ($model->save()) {

                $url = Yii::app()->createUrl('gian-hang-menu');
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdateMenu($id) {
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_Menushop($md->decrypt($id));
        if (isset($_POST['ShopMenu'])) {
            $model->attributes = $_POST['ShopMenu'];
            if ($model->save()) {

                $url = Yii::app()->createUrl('gian-hang-menu');
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteMenu($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_Menushop($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();

        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('gian-hang-menu');
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('gian-hang-menu');
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionListCategory() {
        $this->pageTitle = 'Quản lý danh mục';
        $this->render('//manager/ca-nhan', array(
        ));
    }

    public function actionDsCategory($id) {

        $session = new CHttpSession;
        $session->open();

        $model = ShopNavbar::model()->findAll('flag in(0,1) and shopid=' . $id . ' and uid=' . $session['user_salesmart'][3]);

        $this->pageTitle = 'Thêm danh mục';
        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionAddCategory() {

        $model = new ShopNavbar();
        $rnd = rand(0, 99999);
        $this->pageTitle = 'Thêm danh mục';
        if (isset($_POST['ShopNavbar'])) {

            $model->attributes = $_POST['ShopNavbar'];
            $uploadedFile = CUploadedFile::getInstance($model, 'image');
            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
            if (!empty($uploadedFile)) {
                $model->image = $fileName;
            }


            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];
            //if ($session['user_salesmart'][1] < 99) {
            // $model->flag = 0;
            // }

            if ($model->save()) {
                $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/icon/' . $model->image);
                $url = Yii::app()->createUrl('danh-sach-danh-muc/' . $model->shopid);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdateCategory($id) {
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_shopCategory($md->decrypt($id));
        $rnd = rand(0, 99999);
        $oldphoto = $model->image;
        if (isset($_POST['ShopNavbar'])) {
            $model->attributes = $_POST['ShopNavbar'];
            $uploadedFile = CUploadedFile::getInstance($model, 'image');
            $model->image = $oldphoto;
            $up = 0;
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->image = $fileName;
            }
            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/icon/' . $model->image);
                }
                $url = Yii::app()->createUrl('danh-sach-danh-muc/' . $model->shopid);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteCategory($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_shopCategory($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();

        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-danh-muc/' . $model->shopid);
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-danh-muc/' . $model->shopid);
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionListSlider() {
        $this->pageTitle = 'Quản lý danh mục';
        $this->render('//manager/ca-nhan', array(
        ));
    }

    public function actionDsSlider($id) {

        $session = new CHttpSession;
        $session->open();

        $model = ShopSlider::model()->findAll('flag in(0,1) and shopid=' . $id . ' and uid=' . $session['user_salesmart'][3]);

        $this->pageTitle = 'Thêm danh mục';
        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionAddSliderShop() {

        $model = new ShopSlider;
        $rnd = rand(0, 99999);
        $ketqua = 0;
        $this->pageTitle = 'Tạo slider';
        if (isset($_POST['ShopSlider'])) {

            $model->attributes = $_POST['ShopSlider'];
            $uploadedFile = CUploadedFile::getInstance($model, 'photo');
            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
            if (!empty($uploadedFile)) {
                $model->photo = $fileName;
            }

            $model->dateCreate = new CDbExpression('NOW()');
            $model->money = 0;
            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];

            if ($model->save()) {
                $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/slider/' . $model->photo);

                $ketqua = 1;

                $url = Yii::app()->createUrl('danh-sach-slider-shop/' . $model->shopid);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdateSliderShop($id) {
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_slidershop($md->decrypt($id));

        $rnd = rand(0, 99999);
        $oldphoto = $model->photo;
        $up = 0;
        if (isset($_POST['ShopSlider'])) {
            $model->attributes = $_POST['ShopSlider'];
            $uploadedFile = CUploadedFile::getInstance($model, 'photo');
            $model->photo = $oldphoto;
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->photo = $fileName;
            }

            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/upload/product/slider/' . $model->photo);
                }
                $url = Yii::app()->createUrl('danh-sach-slider-shop/' . $model->shopid);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteSliderShop($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_slidershop($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        //$model->uid = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-slider-shop/' . $model->shopid);
            $this->redirect($url);
        }
        $url = Yii::app()->createUrl('danh-sach-slider-shop/' . $model->shopid);
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionListpanelshop() {
        $this->pageTitle = 'Quản lý quảng cáo';
        $this->render('//manager/ca-nhan', array(
        ));
    }

    public function actionDsPanelshop($id) {
        $criteria = new CDbCriteria();
        $session = new CHttpSession;
        $session->open();

        $userID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] < 99) {
            $criteria->condition = ' flag<2 and uid =' . $userID . ' and shopid=' . $id;
        } else {
            $criteria->condition = ' flag <3 and shopid=' . $id;
        }
        $model = ShopPanel::model()->findAll($criteria);
        $this->render('//manager/ca-nhan', array(
            'model' => $model,));
    }

    public function actionAddPanelshop() {

        $model = new ShopPanel();
        $rnd = rand(0, 99999);
        $ketqua = 0;
        $this->pageTitle = 'Tạo panel';
        if (isset($_POST['ShopPanel'])) {

            $model->attributes = $_POST['ShopPanel'];
            $uploaded = CUploadedFile::getInstance($model, 'photo');
            $fileName = "{$rnd}-{$uploaded}";  // random number + file name
            if (!empty($uploaded)) {
                $model->photo = $fileName;
            }

            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];


            if ($model->save()) {
                $uploaded->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/quangcao/' . $model->photo);
                $ketqua = 1;
                $url = Yii::app()->createUrl('danh-sach-banner-shop/' . $model->id);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdatePanelshop($id) {
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_panelshop($md->decrypt($id));

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        $rnd = rand(0, 99999);
        $oldphoto = $model->photo;
        $up = 0;
        if (isset($_POST['ShopPanel'])) {
            $model->attributes = $_POST['ShopPanel'];
            $uploadedFile = CUploadedFile::getInstance($model, 'photo');
            $model->photo = $oldphoto;
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->photo = $fileName;
            }

            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/quangcao/' . $model->photo);
                }
                $url = Yii::app()->createUrl('danh-sach-banner-shop/' . $model->id);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeletePanelshop($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_panelshop($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        // $model->uid = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-banner-shop/' . $model->id);
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-banner-shop/' . $model->id);
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionListProductshop() {
        $this->pageTitle = 'Quản lý sản phẩm';
        $this->render('//manager/ca-nhan', array(
        ));
    }

    public function actionDsProductshop($id) {

        $criteria = new CDbCriteria();
        $session = new CHttpSession;
        $session->open();

        $userID = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] < 99) {
            $criteria->condition = ' flag<2 and uid =' . $userID . ' and shopid=' . $id;
        } else {
            $criteria->condition = ' flag <3 and shopid=' . $id;
        }
        $model = ShopProduct::model()->findAll($criteria);

        $this->pageTitle = 'Danh sách sản phẩm';
        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionAddProductshop() {

        $model = new ShopProduct();
        $rnd = rand(0, 99999);
        $ketqua = 0;
        $this->pageTitle = 'Tạo sản phẩm';
        if (isset($_POST['ShopProduct'])) {

            $model->attributes = $_POST['ShopProduct'];
            $uploaded = CUploadedFile::getInstance($model, 'avatar');
            $fileName = "{$rnd}-{$uploaded}";  // random number + file name
            if (!empty($uploaded)) {
                $model->avatar = $fileName;
            }

            $model->pr_datecreate = new CDbExpression('NOW()');
            $model->pr_dateup = new CDbExpression('NOW()');
            $model->pr_stardate = new CDbExpression('NOW()');
            //$model->dateCreate = new CDbExpression('NOW()');
            //$model->money = 0;
            $session = new CHttpSession;
            $session->open();

            $model->uid = $session['user_salesmart'][3];
            if ($session['user_salesmart'][1] < 98) {
                $model->flag = 0;
            }

            if ($model->save()) {
                $uploaded->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/product/' . $model->avatar);
                $ketqua = 1;
                $url = Yii::app()->createUrl('danh-sach-product-shop/' . $model->shopid);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionUpdateProductshop($id) {
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_productshop($md->decrypt($id));

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        $rnd = rand(0, 99999);
        $oldphoto = $model->avatar;
        $up = 0;
        if (isset($_POST['ShopProduct'])) {
            $model->attributes = $_POST['ShopProduct'];
            $uploadedFile = CUploadedFile::getInstance($model, 'avatar');
            $model->avatar = $oldphoto;
            $session = new CHttpSession;
            $session->open();

            //$model->uid = $session['user_salesmart'][3];
            if (!empty($uploadedFile)) {
                $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
                $up = 1;
                $model->avatar = $fileName;
            }

            if ($model->save()) {
                if ($up > 0) {
                    $uploadedFile->saveAs(Yii::getPathOfAlias('webroot') . '/uploadshop/product/' . $model->avatar);
                }
                $url = Yii::app()->createUrl('danh-sach-product-shop/' . $model->shopid);
                $this->redirect($url);
            }
        }

        $this->render('//manager/ca-nhan', array(
            'model' => $model,
        ));
    }

    public function actionDeleteProductshop($id) {
        $xoa = 'Không xóa được! Vui lòng liên hệ admin ';
        Yii::import('application.controllers.encode_decode');
        $md = new encode_decode();
        $model = $this->loadModel_productshop($md->decrypt($id));
        $session = new CHttpSession;
        $session->open();
        $model->uid = $session['user_salesmart'][3];
        if ($session['user_salesmart'][1] > 98) {
            $model->flag = 3;
        } else {
            $model->flag = 2;
        }

        if ($model->save()) {
            $url = Yii::app()->createUrl('danh-sach-product-shop/' . $model->shopid);
            $this->redirect($url);
        }

        $url = Yii::app()->createUrl('danh-sach-product-shop/' . $model->shopid);
        $this->redirect($url);
        $this->render('//manager/ca-nhan', array(
            'xoa' => $xoa,
        ));
    }

    public function actionCauhinh1() {
       $id=1;
        $model = $this->loadModel_cauhinh($id);
        $session = new CHttpSession;
        $session->open();
      
        if ($session['user_salesmart'][1] <99) {
             $this->redirect('http://luatsukhongbiengioi.org/');
        }
        if (isset($_POST['Configinfo'])) {
            $model->attributes = $_POST['Configinfo'];
         
            if ($model->save()) {
                $url = Yii::app()->createUrl('ca-nhan');
                $this->redirect($url);
            }
        }

        $this->render('ca-nhan', array(
            'model' => $model,
        ));
    }
    
    public function loadModel_cauhinh($id) {
        $model = Configinfo::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }
    
     public function loadModel_lienheluatsu($id) {
        $model = Lienhe::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }
    
    public function loadModel_lienhequangcao($id) {
        $model = Lienhequangcao::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }
    
    public function loadModel_panelshop($id) {
        $model = ShopPanel::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_pageshop($id) {
        $model = ShopPage::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_Cityshop($id) {
        $model = ShopCity::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_Menushop($id) {
        $model = ShopMenu::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }
    
    public function loadModel_parne($id) {
        $model = Parner::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }


    public function loadModel_slidershop($id) {
        $model = ShopSlider::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_productshop($id) {
        $model = ShopProduct::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_shop($id) {
        $model = Shop::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_Panner($id) {
        $model = Parner::model()->findByUrl($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function loadModel_shopCategory($id) {
        $model = ShopNavbar::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function actionDynamiccities() {
        $data = City::model()->findAll('locaID=:locaID', array(':locaID' => (int) $_POST['id']));
        $session = new CHttpSession;
        $session->open();
        $session['kkl'] = "vao rui do ma";
        $value0 = 0;
        $data = CHtml::listData($data, 'cityID', 'cityName');
        echo CHtml::tag('option', array('value' => $value0), '----- Chọn tỉnh thành -----', true);
        foreach ($data as $value => $name) {
            echo CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
        }
        /*
          if (!empty($data)) {
          foreach ($data as $item) {
          echo CHtml::tag('option', array('value' => $item->disID), $item->disName, true);
          }
         */
    }

    public function actionDynamictim() {
        $data = City::model()->findAll('locaID=:locaID', array(':locaID' => (int) $_POST['id']));
        $value0 = 0;
        $data = CHtml::listData($data, 'cityID', 'cityName');
        echo CHtml::tag('option', array('value' => $value0), '----- Chọn menu cha -----', true);
        foreach ($data as $value => $name) {
            echo CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
        }
    }

}
